package admin

import (
	"go-project/common/casbininit"
	"go-project/dto/requrest"
	"go-project/pkg/utils"
	"go-project/service"
	"net/http"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

type AdminController struct {
}

func (admin AdminController) List(c *gin.Context) {
	pagination := requrest.GeneratePaginationFormRequest(c)
	admin_search_req := requrest.AdminSearchRequest{}
	c.ShouldBind(&admin_search_req)
	admin_search_req.Pagination = pagination
	lists, count, err := service.Admin{}.FindAdmins(&admin_search_req)
	if err != nil {
		utils.Error(c, utils.ERROR, "获取数据出错", http.StatusOK)
		return
	}
	utils.PaginationSuccess(c, count, lists, pagination)
}
func (admin AdminController) Add(c *gin.Context) {
	admin_request := &requrest.AdminInsertRequest{}
	errors := admin_request.BindAndValidate(c)
	if errors != nil {
		return
	}
	insert_id, err := service.Admin{}.Add(admin_request)
	if err != nil {
		utils.Error(c, utils.ERROR, "添加失败", http.StatusOK)
		return
	}
	utils.Success(c, gin.H{
		"id": insert_id,
	}, http.StatusCreated)
}
func (AdminController) Update(c *gin.Context) {
	admin_update_req := &requrest.AdminUpdateRequest{}
	err := admin_update_req.BindAndValidate(c)
	if err != nil {
		return
	}
	update_id, err := service.Admin{}.Update(admin_update_req)
	if err != nil {
		utils.Error(c, utils.ERROR, err.Error(), http.StatusOK)
		return
	}
	utils.Success(c, gin.H{
		"id": update_id,
	}, http.StatusOK)
}

func (AdminController) Edit(c *gin.Context) {
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.HTML(http.StatusNotFound, "admin.error", gin.H{
			"error": err.Error(),
		})
		return
	}
	model, err := service.Admin{}.Get(uint(id))
	if err != nil {
		c.HTML(http.StatusBadGateway, "admin.error", gin.H{
			"error": err.Error(),
		})
		return
	}
	roles, err := casbininit.Enforcer.GetRolesForUser(model.Username)
	if err != nil {
		c.HTML(http.StatusBadGateway, "admin.error", gin.H{
			"error": err.Error(),
		})
		return
	}
	newRoles := make([]string, 0, 10)
	for _, v := range roles {
		newRoles = append(newRoles, strings.TrimLeft(v, "role:"))
	}
	stringRoles := strings.Join(newRoles, ",")
	c.HTML(http.StatusOK, "admin.edit", gin.H{
		"model": model,
		"roles": stringRoles,
	})
}

func (AdminController) Delete(c *gin.Context) {
	value := c.Query("id")
	if value == "" {
		utils.Error(c, utils.ERROR, "id不能为空", http.StatusOK)
		return
	}
	id, err := strconv.Atoi(value)
	if err != nil {
		utils.Error(c, utils.ERROR, err.Error(), http.StatusOK)
		return
	}
	rowsAffected, err := service.Admin{}.Del(uint(id))
	if err != nil {
		utils.Error(c, utils.ERROR, err.Error(), http.StatusOK)
		return
	}
	if rowsAffected <= 0 {
		utils.Error(c, utils.ERROR, "删除失败", http.StatusOK)
	}
	utils.Success(c, gin.H{}, http.StatusNoContent)
}

//批量删除
func (AdminController) BatchDelete(c *gin.Context) {
	value := c.Query("ids")
	if value == "" {
		utils.Error(c, utils.ERROR, "id不能为空", http.StatusOK)
		return
	}
	ids := strings.Split(value, ",")
	var new_ids []int
	for _, id := range ids {
		id, _ := strconv.Atoi(id)
		new_ids = append(new_ids, id)
	}
	rowsAffected, err := service.Admin{}.BatchDelByIds(new_ids)
	if err != nil {
		utils.Error(c, utils.ERROR, err.Error(), http.StatusOK)
		return
	}
	if rowsAffected <= 0 {
		utils.Error(c, utils.ERROR, "删除失败", http.StatusOK)
	}
	utils.Success(c, gin.H{}, http.StatusNoContent)
}
func (AdminController) DistributeRole(c *gin.Context) {
	if c.Request.Method == "POST" {
		adminDistributeRoleRequest := &requrest.AdminDistributeRoleRequest{}
		err := adminDistributeRoleRequest.BindAndValidate(c)
		if err != nil {
			return
		}

		err = service.Admin{}.DistributeRole(adminDistributeRoleRequest)
		if err != nil {
			utils.Error(c, utils.ERROR, err.Error(), http.StatusOK)
			return
		}
		utils.Success(c, gin.H{}, http.StatusNoContent)
		return
	}
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		c.HTML(http.StatusNotFound, "admin.error", gin.H{
			"error": err.Error(),
		})
		return
	}
	model, err := service.Admin{}.Get(uint(id))
	if err != nil {
		c.HTML(http.StatusBadGateway, "admin.error", gin.H{
			"error": err.Error(),
		})
		return
	}
	roles, err := casbininit.Enforcer.GetRolesForUser(model.Username)
	if err != nil {
		c.HTML(http.StatusBadGateway, "admin.error", gin.H{
			"error": err.Error(),
		})
		return
	}
	newRoles := make([]string, 0, 10)
	for _, v := range roles {
		newRoles = append(newRoles, strings.TrimLeft(v, "role:"))
	}
	stringRoles := strings.Join(newRoles, ",")

	c.HTML(http.StatusOK, "admin.distribute-role", gin.H{
		"model": model,
		"roles": stringRoles,
	})

}
